package com.aivin.mobileApi;

import com.aivin.entity.TbUserV1;
import com.aivin.service.PicService;
import com.aivin.service.UserService;
import com.aivin.txSms.SmsSender;
import com.aivin.utils.MyStringUtils;
import com.aivin.utils.MyTimeUtils;
import com.aivin.utils.MyValidateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 更新用户的信息。
 * 更新头像、密码、昵称 等等
 */
@Controller//@Service @Component
@RequestMapping("/userUpdateApi")
public class UpDateUserController {


    /**
     * 验证码有效期
     */
    public final int ValidTime = 5;
    @Autowired
    UserService userService;


    /***
     * 更新用户头像
     * http://192.168.0.119:8081/findYou/userUpdateApi/upDateUserImgUrl
     * @param userId 用户id
     * @param imgUrl 用户更新的头像
     * @return 只支持post 。
     */
    @RequestMapping(value = "/upDateUserImgUrl", method = {RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String upDateUserImgUrl(@RequestParam("userId") int userId, @RequestParam("imgUrl") String imgUrl) {
        System.out.println(">>>>>>>  " + imgUrl + "  " + userId);
        String jsonStr = "noData";
        int updateResult = userService.upDateUserImgUrl(userId, imgUrl);

        if (updateResult > 0) {
            jsonStr = "success";
        } else {
            jsonStr = "failed";
        }

        System.out.println("upDateUserImgUrl#######  " + updateResult + "  " + jsonStr);


        return jsonStr;
    }




    /***
     * 绑定、更新用户的电话号码
     * http://192.168.0.102:8081/findYou/userUpdateApi/bindOrUpDateUserTel
     * 只支持post 。
     * @param userId 用户id
     * @param userTel 用户更新tel
     * @return
     * jsonStr = "success";
     * jsonStr = "failed";
     */
    @RequestMapping(value = "/bindOrUpDateUserTel", method = {RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String bindOrUpDateUserTel(@RequestParam("userId") int userId, @RequestParam("userTel") String userTel) {
        System.out.println(">>>>>>>  " + userTel + "  " + userId);
        String jsonStr = "noData";
        int updateResult = userService.bindOrUpDateUserTel(userId, userTel);

        if (updateResult > 0) {
            jsonStr = "success";
        } else {
            jsonStr = "failed";
        }

        System.out.println("bindOrUpDateUserTel#######  " + updateResult + "  " + jsonStr);


        return jsonStr;
    }

    /***
     * 更新昵称
     * http://192.168.0.102:8081/findYou/userUpdateApi/upDateUserNickeName
     * @param userId 用户id
     * @param nickName  用户昵称。
     * @return 只支持post 。
     */
    @RequestMapping(value = "/upDateUserNickeName", method = {RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String upDateUserNickeName(@RequestParam("userId") int userId, @RequestParam("nickName") String nickName) {
        System.out.println(">>>>>>>  " + nickName + "  " + userId);
        String jsonStr = "noData";
        int updateResult = userService.upDateUserNickeName(userId, nickName);

        if (updateResult > 0) {
            jsonStr = "success";
        } else {
            jsonStr = "failed";
        }

        System.out.println("upDateUserNickeName#######  " + updateResult + "  " + jsonStr);


        return jsonStr;
    }

    /***
     * 更新密码
     * http://192.168.0.102:8081/findYou/userUpdateApi/upDateUserPwd
     * @param userId 用户id
     * @param userPwdOld  旧密码。 修改密码之前需要先确认一下旧密码是否正确。 修改密码不需要验证手机。在登陆状态下验证原始密码即可。
     * @param userPwdNew  新密码 。
     * @return 只支持post 。
     */
    @RequestMapping(value = "/upDateUserPwd", method = {RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String upDateUserPwd(@RequestParam("userId") int userId,  @RequestParam("userPwdOld") String userPwdOld ,@RequestParam("userPwdNew") String userPwdNew ) {
        System.out.println(">>>>>>>  " + userPwdNew + "  " + userId +" "+userPwdOld  );
        String jsonStr = "noData";
        // 修改密码之前需要先确认一下旧密码是否记得。防止别人乱修改。
        TbUserV1 bean = userService.getUserInfoByUserIdV1(userId) ;
        if(MyValidateUtil.isEmpty(bean)){
            return "查询异常1003" ;
        }

        if(!userPwdOld.equals(bean.getPwd())){
            return "原密码不正确。不能修改" ;
        }


        int updateResult = userService.upDateUserPwd(userId, userPwdNew);
        if (updateResult > 0) {
            jsonStr = "success";
        } else {
            jsonStr = "failed";
        }

        System.out.println("upDateUserNickeName#######  " + updateResult + "  " + jsonStr);


        return jsonStr;
    }


    /***
     * 重置密码
     * http://192.168.0.102:8081/findYou/userUpdateApi/reSetUserPwd
     * 只支持post 。
     * @param userId 用户id
     * @param userPwdNew  新密码
     * @return
     *jsonStr = "success";
     *jsonStr = "failed";
     */
    @RequestMapping(value = "/reSetUserPwd", method = {RequestMethod.POST}, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String reSetUserPwd(@RequestParam("userId") int userId ,@RequestParam("userPwdNew") String userPwdNew ) {
        System.out.println(">>>>>>>  " + userPwdNew + "  " + userId     );
        String jsonStr = "noData";

        int updateResult = userService.upDateUserPwd(userId, userPwdNew);
        if (updateResult > 0) {
            jsonStr = "success";
        } else {
            jsonStr = "failed";
        }

        System.out.println("reSetUserPwd#######  " + updateResult + "  " + jsonStr);
        return jsonStr;
    }


}
